package com.cjm.one.order.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjm.one.model.order.dto.OrderChartItem;
import com.cjm.one.model.order.dto.OrderSummary;
import com.cjm.one.model.order.dto.TimeRange;
import com.cjm.one.model.order.entity.OrderInfo;
import com.cjm.one.model.order.enums.OrderStatusEnum;
import com.cjm.one.model.order.enums.PayTypeEnum;
import com.cjm.one.model.order.vo.OrderInfoVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
* @author 29289
* @description 针对表【order_info(订单)】的数据库操作Mapper
* @createDate 2024-09-05 16:11:25
* @Entity com.one.order.entity.OrderInfo
*/
public interface OrderInfoMapper extends BaseMapper<OrderInfo> {

    OrderInfoVo selectOrderInfoById(Long id);


    List<OrderInfoVo> selectPageOrderInfoVo(
            @Param("offset") Long offset,
            @Param("size") Long size,
            @Param("userId") Long userId,
            @Param("orderNo") String orderNo,
            @Param("status") OrderStatusEnum status,
            @Param("payStatus") PayTypeEnum payStatus,
            @Param("userName") String userName,
            @Param("createTimeStart") String createTimeStart,
            @Param("createTimeEnd") String createTimeEnd,
            @Param("phone") String phone
    );

    Long selectPageOrderCount(
            @Param("userId") Long userId,
            @Param("orderNo") String orderNo,
            @Param("status") OrderStatusEnum status,
            @Param("payStatus") PayTypeEnum payStatus,
            @Param("userName") String userName,
            @Param("createTimeStart") String createTimeStart,
            @Param("createTimeEnd") String createTimeEnd,
            @Param("phone") String phone
    );

    Page<OrderInfoVo> selectPageVo(Page<OrderInfoVo> voPage, @Param("statusList") List<Integer> statusList,@Param("customId") Long id);

    int updateOrderById(OrderInfoVo orderInfoVo);


    @Select("SELECT " +
            "SUM(CASE WHEN order_status NOT IN (8) THEN total_amount ELSE 0 END) AS totalAmount, " +
            "COUNT(CASE WHEN order_status = 4 THEN id END) AS orderCount, " +
            "SUM(CASE WHEN order_status IN (5,6,7) THEN refund_amount ELSE 0 END) AS refundAmount, " +
            "COUNT(CASE WHEN order_status IN (5,6,7) THEN id END) AS refundOrderCount " +
            "FROM order_info " +
            "WHERE create_time BETWEEN #{start} AND #{end}")
    OrderSummary selectOrderSummary(@Param("start") LocalDateTime start,
                                    @Param("end") LocalDateTime end);

    @Select("SELECT DATE_FORMAT(create_time,'%Y-%m-%d') AS date, " +
            "SUM(CASE WHEN order_status NOT IN (8) THEN total_amount ELSE 0 END) AS amount, " +
            "COUNT(CASE WHEN order_status NOT IN (8) THEN id END) AS count " +
            "FROM order_info " +
            "WHERE create_time BETWEEN #{start} AND #{end} " +
            "GROUP BY date " +
            "ORDER BY date")
    List<OrderChartItem> selectOrderChartData(@Param("start") LocalDateTime start,
                                              @Param("end") LocalDateTime end);
    //查询时间段内订单金额
    BigDecimal selectOrderAmount(@Param("range") TimeRange range);
    //查询时间段内订单数量
    Long selectOrderCount(@Param("range") TimeRange range);
    //查询时间段内退款金额
    BigDecimal selectRefundAmount(@Param("range") TimeRange range);
    //查询时间段内退款订单数量
    Long selectRefundOrderCount(@Param("range") TimeRange range);

    OrderInfoVo selectByNoVo(String orderNo);
}




